/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.mikepenz.actionitembadge.library.utils;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class Logger {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 110, "seagazer");

    private static String replaceFormat(String logMessageFormat) {
        return logMessageFormat.replaceAll("%([d|f|s])", "%{public}$1");
    }

    /***
     * 设置info信息
     * @param message 信息
     * @param args 具体内容
     ***/
    public static void info(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.info(LABEL, replaceFormat(message), args);
        }
    }

    /***
     * 设置debug信息
     * @param message 信息
     * @param args 具体内容
     ***/
    public static void debug(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.debug(LABEL, replaceFormat(message), args);
        }
    }

    /***
     * 设置warn信息
     * @param message 信息
     * @param args 具体内容
     ***/
    public static void warn(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.warn(LABEL, replaceFormat(message), args);
        }
    }

    /***
     * 设置error信息
     * @param message 信息
     * @param args 具体内容
     ***/
    public static void error(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.error(LABEL, replaceFormat(message), args);
        }
    }

    /***
     * 设置fatel信息
     * @param message 信息
     * @param args 具体内容
     ***/
    public static void fatal(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.fatal(LABEL, replaceFormat(message), args);
        }
    }
}
